home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15514 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: crl.crl.com!not-for-mail
  2. From: bobfry@crl.com (Robert Fry)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Memory leakage
  5. Date: 19 Apr 1996 08:30:00 -0700
  6. Organization: CRL Dialup Internet Access
  7. Message-ID: <4l8bho$6m8@crl.crl.com>
  8. References: <4l3582$1v@alice.walrus.com>
  9. NNTP-Posting-Host: crl.com
  10.  
  11. warrenj@walrus.com (Warren Johnson) writes:
  12.  
  13. >Suppose I have a function:
  14.  
  15. >int give_number() {
  16. >   ...
  17. >   ...
  18. >   ...
  19. >}
  20.  
  21. >and in main I have a line :
  22.  
  23. >if(x>give_number()) { 
  24. >  ...
  25. >  ... }
  26.  
  27. >Now my question is this:  give_number() returns an integer.  Obviously 
  28. >the compiler must allocate memory for this integer.  However, we are not 
  29. >returning this integer to an integer pointer, therefore this allocated 
  30. >memory is floating about in the void somewhere without something pointing 
  31. >to it.  Is this memory deallocated when the function is finished running 
  32. >it's course or do i have to do this:
  33.  
  34. Actually, most compilers don't need to allocate memory for this integer. 
  35. They do need to keep track of where it's stored (in most computers I've 
  36. used, it's either in a register on the CPU or on the top of the stack). 
  37. However, both of these locations are quite volatile; most compilers have 
  38. no trouble reusing those spaces when you get to the next statement -- 
  39. there is no need to worry about allocation and freeing of space /in this 
  40. circumstance/.
  41.  
  42. Now, should you want to use the value returned by give_number() 
  43. elsewhere, you will need to store it someplace you can get back to -- 
  44. such as a variable or a dynamically allocated space.
  45.  
  46. Hope that helps!
  47.   Bob
  48.